/**
 * @author SaoE
 * @date 2025/3/25 16:41
 */
public class _2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int plus = 0;
        ListNode head = new ListNode(0);
        ListNode h = head;
        while (l1 != null || l2 != null){
            int res = 0;
            if (l1 != null && l2 != null){
                res = l1.val + l2.val + plus;
                if (res >= 10){
                    plus = 1;
                    res -= 10;
                }else {
                    plus = 0;
                }
            }else if (l1 != null){
                res = l1.val + plus;
                if (res >= 10){
                    plus = 1;
                    res -= 10;
                }else {
                    plus = 0;
                }
            }else if (l2 != null){
                res = l2.val + plus;
                if (res >= 10){
                    plus = 1;
                    res -= 10;
                }else {
                    plus = 0;
                }
            }
            ListNode temp = new ListNode(res);
            head.next = temp;
            head = head.next;

            System.out.print(res);
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
        }
        if(plus == 1){
            ListNode temp = new ListNode(1);
            head.next = temp;
        }
        return h.next;
    }
}
